package com.hxgis.flow.handler;

import com.hxgis.common.entity.Station;
import com.hxgis.common.service.StationService;
import com.hxgis.common.utils.CollectionUtils;
import com.hxgis.common.utils.RectField;
import com.hxgis.common.utils.TimeUtil;
import com.iias.common.context.Tuple;
import com.iias.task.flow.TaskExecuteData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Component
public class YearStationDivideHandler implements TaskExecuteData {

    @Autowired
    private StationService stationService;


    @Override
    public List<Tuple> execute(Tuple tuple) {
        String startTime = tuple.getFirst();
        String endTime = tuple.getSecond();
        Integer stationNum = tuple.getThird();
        Integer startYear = Integer.parseInt(startTime.substring(0,4));
        Integer endYear = Integer.parseInt(endTime.substring(0,4));

        List<Station> stationInfos = stationService.selectAll();
        List<String> stationNos = RectField.getBeanFields(stationInfos, "stationNo");
        // 切分
        List<String>[] sNosList = CollectionUtils.subArrays(stationNos,stationNum);
        List<Tuple> tuples = new ArrayList<Tuple>();

        // 时间 站点切分
        for (int i = startYear; i <= endYear; i++){
            if (Objects.equals(startYear,endYear) || Objects.equals(startYear, i) || Objects.equals(endYear,i)){
                String stTime = null;
                String edTime = null;
                Integer startMonth = null;
                Integer endMonth = null;
                if (Objects.equals(startYear,endYear)){
                    stTime = startTime;
                    startMonth = Integer.parseInt(startTime.substring(4,6));
                    endMonth =  Integer.parseInt(endTime.substring(4,6));

                    // 循环月
                    for (List<String> stationNoList : sNosList) {
                        get(startTime, endTime, i, stTime, startMonth, endMonth, stationNoList, tuples);
                    }
                    continue;
                }
                if (Objects.equals(startYear, i)){
                    stTime = startTime;
                    startMonth = Integer.parseInt(startTime.substring(4,6));
                    endMonth =  12;

                    // 循环月
                    for (List<String> stationNoList : sNosList) {
                        get(startTime, endTime, i, stTime, startMonth, endMonth, stationNoList, tuples);
                    }
                    continue;
                }
                if (Objects.equals(endYear,i)){
                    stTime = endYear + "0101";
                    startMonth = 1;
                    endMonth =  Integer.parseInt(endTime.substring(4,6));
                    // 循环月
                    for (List<String> stationNoList : sNosList) {
                        get(startTime, endTime, i, stTime, startMonth, endMonth, stationNoList, tuples);
                    }
                    continue;
                }
            } else {
                for (int j = 1; j <= 12 ; j++){
                    String firstDayOfMonth = TimeUtil.getFirstDayOfMonth(i, j);
                    String lastDayOfMonth = TimeUtil.getLastDayOfMonth(i, j);
                    for (List<String> stationNoList : sNosList) {
                        tuples.add(Tuple.instance(firstDayOfMonth, lastDayOfMonth, stationNoList).putTriggerVal("index",1));
                    }
                }
            }
        }
        // 只进行时间切分
        for (int i = startYear; i <= endYear; i++){
            // 这里需要处理一下
            if (Objects.equals(startYear,endYear) || Objects.equals(startYear, i) || Objects.equals(endYear,i)){
                if (Objects.equals(startYear,endYear)){
                    tuples.add(Tuple.instance(startTime, i + "1231").putTriggerVal("index",2));
                    continue;
                }
                if (Objects.equals(startYear, i)){
                    tuples.add(Tuple.instance(startTime, TimeUtil.getLastDayOfMonth(i, 12)).putTriggerVal("index",2));
                    continue;
                }
                if (Objects.equals(endYear,i)){
                    tuples.add(Tuple.instance(TimeUtil.getFirstDayOfMonth(i, 1), TimeUtil.getLastDayOfMonth(i, 12)).putTriggerVal("index",2));
                    continue;
                }
            }else {
                tuples.add(Tuple.instance(i + "0101", i + "1231").putTriggerVal("index",2));
            }
        }

        // 只进行站点切分
        for (List<String> stationNoList : sNosList) {
            tuples.add(Tuple.instance(stationNoList).putTriggerVal("index",3));
        }
        return tuples;
    }


    private static void get(String startTime, String endTime, int i, String stTime, Integer startMonth, Integer endMonth, List<String> stationNoList, List<Tuple> tuples) {
        for (int j = startMonth; j <= endMonth; j++) {
            if (Objects.equals(Integer.parseInt(startTime.substring(4,6)),j)){
                tuples.add(Tuple.instance(stTime, TimeUtil.getLastDayOfMonth(i,j), stationNoList).putTriggerVal("index",1));
            }else if (Objects.equals(Integer.parseInt(endTime.substring(4,6)),j)){
                tuples.add(Tuple.instance(TimeUtil.getFirstDayOfMonth(i,j),endTime,stationNoList).putTriggerVal("index",1));
            }else{
                tuples.add(Tuple.instance(TimeUtil.getFirstDayOfMonth(i,j),TimeUtil.getLastDayOfMonth(i,j),stationNoList).putTriggerVal("index",1));
            }
        }
    }


}
